package com.pek.security;

import com.pek.control.entity.UserEntity;
import com.pek.control.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class MyDetailService implements UserDetailsService {
    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity user = userService.getUser(username);
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setUserEntity(user);
        return loginUser;
    }
}
